package com.yilian.xlmusic.android.view.bean;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.yilian.xlmusic.android.R;
import com.yilian.xlmusic.android.base.MyApplication;
import com.yilian.xlmusic.android.utils.HistoryRecord;
import com.yilian.xlmusic.android.utils.music.DiffDemoCallback;
import com.yilian.xlmusic.android.utils.net.AppConstants;
import com.yilian.xlmusic.android.view.adapter.HistoryAdapter;
import com.yilian.xlmusic.android.view.adapter.ItemRecycleViewAdapter;
import java.util.List;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import rxhttp.RxHttp;

public class HistoryPresenter {
    public  void showHistory(Context context, RecyclerView recycler_list, SharedViewModel model) {
        List<String> stringList = HistoryRecord.getSearchHistory(context);
        HistoryAdapter historyAdapter = new HistoryAdapter(R.layout.item_history, stringList,model);
        recycler_list.setAdapter(historyAdapter);
        if (stringList.size() >= 3) {
            View view = View.inflate(context, R.layout.search_history_clear, null);
            TextView clear = view.findViewById(R.id.history_clear);
            clear.setOnClickListener(v -> {
                HistoryRecord.deleteSearchHistoryAll(context);
                showHistory(context,recycler_list, model);
            });
            historyAdapter.addFooterView(view);
        }
    }

    /**
     * @param value 搜索关键字
     */
    public void goSearch(Context context,String value,SharedViewModel model,RecyclerView recycler_list) {
        AppConstants.METHOD_NAME = "搜索关键字：";
        RxHttp.get("/ylmusic-gateway/search/result")
                .addHeader(AppConstants.ACCESS_TOKEN_KEY, AppConstants.ACCESS_TOKEN)
                .add("keywords", value)
                .addEncoded("=", "a")
                .asClass(MusicListBean.class)
                .observeOn(AndroidSchedulers.mainThread())  //主线程回调观察者
                .subscribe(s -> {
                    MusicListBean.DataBean data = s.getData();
                    SharedMessage message = new SharedMessage();
                    message.setType(3);
                    message.setValue(data.getKeywords());
                    model.select(message);

                    List<MusicPlayerBean.DataBean> dataList = data.getList();
                    ItemRecycleViewAdapter adapter2 = new ItemRecycleViewAdapter(R.layout.music_player_item2, dataList,true);
                    adapter2.setDiffCallback(new DiffDemoCallback());
                    recycler_list.setAdapter(adapter2);
                    adapter2.setEmptyView(R.layout.search_empty);
                    if (dataList.size()>10){
                        View inflate = View.inflate(context, R.layout.none, null);
                        adapter2.addFooterView(inflate);
                    }
                }, throwable -> {
                    Log.e("请求失败了", "请求失败了" + throwable);

                });
    }

    public void HistoryItemClick(RecyclerView recyclerView,String searchValue,SharedViewModel model){
        HistoryRecord.deleteSearchHistory(MyApplication.getContext(), searchValue);
        showHistory(MyApplication.getContext(),recyclerView, model);
    }


}
